﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Logistics.DataManage.DAL;
using MapgisEbp.QuickDataBase;
using Logistics.DataManage.Local;
using System.Data;

namespace Logistics.DataManage
{
    class RouteDAO
    {
        public static ORoute GetRouteById(String RouteId)
        {
            String sql = "select * from Routes where RoutesID=? ";
            ParameterizedSQL PSQL = LocalDb.Handler.DbAdapter.AdaptSQLAnonymousParams(sql, RouteId);
            DataSet ds = LocalDb.Handler.ExecuteQuery(PSQL.SQL, PSQL.Parameters);
            IList<ORoute> list = ConvertTo(ds);
            return list.Count == 0 ? null : list[0];
        }
        public static ORoute GetRouteByStartEndId(String startSiteId,String endSiteId)
        {
            String sql = "select * from Routes where BeginSiteID=? and EndSiteID=? ";
            ParameterizedSQL PSQL = LocalDb.Handler.DbAdapter.AdaptSQLAnonymousParams(sql, startSiteId, endSiteId);
            DataSet ds = LocalDb.Handler.ExecuteQuery(PSQL.SQL, PSQL.Parameters);
            IList<ORoute> list = ConvertTo(ds);
            return list.Count == 0 ? null : list[0];
        }

        public static String GetRouteIdByStartEndId(String startSiteId, String endSiteId)
        {
            String sql = "select RoutesID from Routes where BeginSiteID=? and EndSiteID=? ";
            ParameterizedSQL PSQL = LocalDb.Handler.DbAdapter.AdaptSQLAnonymousParams(sql, startSiteId, endSiteId);
            Object obj = LocalDb.Handler.ExecuteScalar(PSQL.SQL, PSQL.Parameters);
            return obj == null ? "" : obj.ToString();
        }

        private static IList<ORoute> ConvertTo(DataSet ds)
        {
            IList<ORoute> list = new List<ORoute>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    ORoute route = new ORoute();
                    route.RouteID = Convert.ToString(row["RoutesID"]);
                    route.RouteName = Convert.ToString(row["RouteName"]);
                    route.FreightRate = Convert.ToString(row["FreightRate"]);
                    route.InsuredFreeRate = Convert.ToString(row["InsuredFreeRate"]);
                    route.StartCollectionRate = Convert.ToString(row["StartCollectionRate"]);
                    route.WarehouseFeeRate = Convert.ToString(row["WarehouseFeeRate"]);

                    //route.EndCollectionRate = Convert.ToSingle(row["EndCollectionRate"]);
                    route.ImprestFeeRate = Convert.ToString(row["EndCollectionRate"]);//预收款费率
                    route.TranSferregisteRate = Convert.ToString(row["ImprestFeeRate"]);//转账登记费率
                    route.DeliveryFee = Convert.ToString(row["TranSferregisteRate"]);//送货费费率

                    route.LiangFangRate = Convert.ToString(row["LiangFangRate"]);
                    route.WeightRates = Convert.ToString(row["WeightRates"]);

                    list.Add(route);
                }
            }
            return list;
        }
    }
}
